package com.ratta.supernote.plugin.sticker.lib.view.editText;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Layout;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;


import androidx.annotation.RequiresApi;

import com.facebook.react.views.textinput.ReactEditText;
import com.ratta.supernote.plugin.sticker.lib.view.editText.editor.CursorRect;
import com.ratta.supernote.plugin.sticker.lib.view.editText.editor.RattaEditor;
import com.ratta.supernote.plugin.sticker.lib.view.editText.editor.RattaTextAPI;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
public class CustomTextInput extends ReactEditText {
    private final String TAG = "CustomTextInput";
    private int action = 1;
    private static final int MAX_TEXT_LENGTH = 150;


    private RattaEditor mRattaEditor;


    public CustomTextInput(Context context) {
        super(context);
        init();

    }


    private void init() {
        Log.i(TAG,"CustomTextInput new");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            disableDragHandles();
        }
        mRattaEditor = new RattaEditor(this);
        InputFilter[] filters = getFilters();
        InputFilter[] filtersNew = Arrays.copyOf(filters, filters.length +1);
        filtersNew[filters.length] = new ByteLengthFilter(MAX_TEXT_LENGTH);
        setFilters(filtersNew);

        // 设置输入框里面内容发生改变的监听
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        /*Editable editable = getText();
        int len = editable.toString().getBytes().length;
        if (len > MAX_TEXT_LENGTH) {
            int selEndIndex = Selection.getSelectionEnd(editable);
            String str = editable.toString();
            //截取新字符串
            String newStr = getWholeText(str, MAX_TEXT_LENGTH);
            text = newStr;
            setText(newStr);
            editable = getText();
            //新字符串的长度
            int newLen = editable.length();
            //旧光标位置超过字符串长度
            if (selEndIndex > newLen) {
                selEndIndex = editable.length();
            }
            //设置新光标所在的位置
            Selection.setSelection(editable, selEndIndex);
        }*/
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void disableDragHandles() {
        Log.i(TAG,"CustomTextInput disableDragHandles");
        // 禁用长按触发文本选择
        setLongClickable(false);
        setTextIsSelectable(false);
        setCursorVisible(false);
        setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });
    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        if (id == android.R.id.cut || id == android.R.id.copy) {
            return false; // 禁用剪切、复制
        }
        return super.onTextContextMenuItem(id);
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onPreDraw() {
        Log.i(TAG,"RattaEditText onPreDraw");
        // 拖动手柄为起始手柄的时候，需要需要将拖动的位置滚到可见区域
        if(mRattaEditor.isDraggingStart) {
            int curs = getSelectionStart();
            bringPointIntoView(curs);
            return true;
        }
        // 在队尾的时候不需要调用onPreDraw，否则会滚动，导致引起问题
        if(mRattaEditor.bringTextIntoView()) {
            return true;
        }
        return super.onPreDraw();
    }


    protected void onDraw(Canvas canvas) {
        mRattaEditor.drawHighlight(canvas);
        super.onDraw(canvas);
        mRattaEditor.drawSelectedText(canvas);
        mRattaEditor.drawCursor(canvas);
    }


    public boolean onTouchEvent(MotionEvent event) {
        action = event.getAction();
        if(mRattaEditor.onTouchEvent(event)) {
            return true;
        }
        if (event.getAction() == 1 && this.getCompoundDrawables()[2] != null) {
            int x = (int)event.getX();
            int y = (int)event.getY();
            boolean isInnerWidth = x > this.getWidth() - this.getTotalPaddingRight() + 24 && x < this.getWidth();
            boolean isInnerHeight = y >= 0 && y < this.getHeight();
            if (isInnerWidth && isInnerHeight) {
                this.requestFocus();
                this.setText("");
            }
        }

        return super.onTouchEvent(event);
    }




    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        // 拦截物理键盘的 Enter 键
        if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
            return true; // 阻止事件传递
        }
        return super.onKeyPreIme(keyCode, event);
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        if(mRattaEditor != null) {
            mRattaEditor.onSelectionChanged(selStart, selEnd);
        }

    }

    public void cancelSelection() {
        if(mRattaEditor != null) {
            mRattaEditor.cancelSelection();
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mRattaEditor != null) {
            mRattaEditor.destroy();
        }
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if(!hasFocus()) {
            cancelSelection();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        Log.i(TAG,"onWindowFocusChanged hasWindowFocus:"+hasWindowFocus);
        clearFocus();
        if(!hasWindowFocus) {
            cancelSelection();
        }
    }

    @SuppressLint("NewApi")
    private static String getWholeText(String text, int byteCount) {
        if (text != null && text.getBytes(StandardCharsets.UTF_8).length > byteCount) {
            char[] tempChars = text.toCharArray();
            int sumByte = 0;
            int charIndex = 0;
            for (int i = 0, len = tempChars.length; i < len; i++) {
                char itemChar = tempChars[i];
                // 根据Unicode值，判断它占用的字节数
                if (itemChar <= 0x007F) {
                    sumByte += 1;
                } else if (itemChar <= 0x07FF) {
                    sumByte += 2;
                } else {
                    sumByte += 3;
                }
                if (sumByte > byteCount) {
                    charIndex = i;
                    break;
                }
            }
            return String.valueOf(tempChars, 0, charIndex);
        }
        return text;
    }


}
